/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.vehicle;

import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeEntity;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class VehicleManagerBean implements VehicleManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addVehicle(VehicleDetailedTO to) throws VehicleManagerException {
        try {
            VehicleEntity v = new VehicleEntity();
            v.setData(to);
            v.setVehicleType(this.em.find(VehicleTypeEntity.class, to.getVehicleType().getId()));
            v.setVehicleLicenceType(this.em.find(VehicleLicenceTypeEntity.class, to.getVehicleLicenceType().getId()));
            v.setDeleted(Boolean.FALSE);
            this.em.persist(v);
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot add vehicle", e);
        }
    }

    public void editVehicle(VehicleDetailedTO to) throws VehicleManagerException {
        try {
            VehicleEntity v = this.em.find(VehicleEntity.class, to.getId());
            v.setData(to);
            v.setVehicleType(this.em.find(VehicleTypeEntity.class, to.getVehicleType().getId()));
            v.setVehicleLicenceType(this.em.find(VehicleLicenceTypeEntity.class, to.getVehicleLicenceType().getId()));
            this.em.merge(v);
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot edit vehicle", e);
        }
    }

    public void deleteVehicle(Long id) throws VehicleManagerException {
        try {
            VehicleEntity v = this.em.find(VehicleEntity.class, id);
            v.setDeleted(Boolean.TRUE);
            this.em.merge(v);
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot delete vehicle", e);
        }
    }

    public List<VehicleTO> getAllVehicles() throws VehicleManagerException {
        try {
            List<VehicleTO> results = new ArrayList<VehicleTO>();
            Query allVehiclesQuery = em.createNamedQuery("findAllVehicles");
            List dbResults = allVehiclesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleEntity v = (VehicleEntity) dbResults.get(i);
                results.add(v.getData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot get all vehicles", e);
        }
    }

    public List<VehicleDetailedTO> getAllDetailedVehicles() throws VehicleManagerException {
        try {
            List<VehicleDetailedTO> results = new ArrayList<VehicleDetailedTO>();
            Query allVehiclesQuery = em.createNamedQuery("findAllVehicles");
            List dbResults = allVehiclesQuery.getResultList();
            for (int i = 0; i < dbResults.size(); i++) {
                VehicleEntity v = (VehicleEntity) dbResults.get(i);
                results.add(v.getDetailedData());
            }
            return results;
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot get all detailed vehicles", e);
        }
    }

    public VehicleDetailedTO getVehicle(Long id) throws VehicleManagerException {
        try {
            VehicleEntity v = em.find(VehicleEntity.class, id);
            return v.getDetailedData();
        } catch (Exception e) {
            throw new VehicleManagerException("Cannot get vehicle by ID " + id.longValue(), e);
        }
    }
}
